from src.db import dbm, dbs
from src.core.variables.status import *


def user_list(schema):
    result = []
    with dbm.get("sys_default") as db_session:
        from sqlalchemy.orm import scoped_session
        db_session: scoped_session
        db_result = db_session.query(dbs.sys_default.User).all()
        for i in db_result:
            temp = {
                "uid": i.uid,
                "username": i.username,
                "email": i.email,
                "phone": i.phone,
                "role": eval(i.role),
                "create_time": i.create_time,
                "update_time": i.update_time,
                "last_login_time": i.last_login_time,
                "last_login_ip": i.last_login_ip,
                "login_count": i.login_count,
            }
        result.append(temp)
        return result


def user_info(schema):
    """获取用户信息"""
    with dbm.get("sys_default") as db_session:
        from sqlalchemy.orm import scoped_session
        db_session: scoped_session
        db_result = (
            db_session.query(dbs.sys_default.User)
            .filter(dbs.sys_default.User.uid == schema.uid)
            .first()
        )
        if db_result:
            result = {
                "uid": db_result.uid,
                "username": db_result.username,
                "email": db_result.email,
                "phone": db_result.phone,
                "role": eval(db_result.roles),
                "create_time": db_result.create_time,
                "update_time": db_result.update_time,
                "last_login_time": db_result.last_login_time,
                "last_login_ip": db_result.last_login_ip,
                "login_count": db_result.login_count,
            }
            return SUCCESS_CODE_CUSTOMIZE(result, 200)
        return ERROR_CODE_CUSTOMIZE("用户不存在", 404)
